package org.farm2.service.event;

import com.farm2.llm.client.inter.Farm2EmbedClientInter;
import com.farm2.lucene.Farm2LuceneFace;
import com.farm2.lucene.document.FarmDocument;
import com.farm2.lucene.impl.Farm2LuceneIndexImpl;
import com.farm2.lucene.inter.Farm2LuceneIndexInter;
import com.farm2.lucene.utils.FarmLuceneUtils;
import lombok.extern.slf4j.Slf4j;
import org.farm2.base.domain.FarmUserContext;
import org.farm2.base.event.F2Event;
import org.farm2.base.event.enums.F2EActionT;
import org.farm2.base.event.enums.F2EObjectT;
import org.farm2.base.event.inter.Farm2EventHandle;
import org.farm2.files.domain.ResourceFile;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.files.utils.Farm2RegisteTypeEnum;
import org.farm2.files.utils.FileDownloadUtils;
import org.farm2.llm.service.LlmClientServiceInter;
import org.farm2.service.event.utils.KnowIndexDocumentBuilder;
import org.farm2.service.inter.FarmUserServiceInter;
import org.farm2.service.lucene.doc.EmbeddingIndexDocument;
import org.farm2.service.lucene.doc.KnowIndexDocument;
import org.farm2.service.lucene.doc.enums.DocumentTitleEnum;
import org.farm2.service.lucene.doc.enums.IndexModelEnum;
import org.farm2.skc.domain.SkcKnow;
import org.farm2.skc.dto.web.IndexTagsDto;
import org.farm2.skc.service.SkcKnowServiceInter;
import org.farm2.tools.files.Farm2FileUtils;
import org.farm2.tools.i18n.I18n;
import org.farm2.tools.web.Farm2HtmlUtils;
import org.farm2.tools.web.domain.TextParagraph;
import org.farm2.wdap.convertor.filemode.impl.SegmentFileMode;
import org.farm2.wdap.convertor.filemode.impl.TextFileMode;
import org.farm2.wdap.service.WdapExtendFileServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建附件索引
 */
@Slf4j
@Service
public class EventLuceneEmbeddingHandle implements Farm2EventHandle {
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private FileDownloadUtils fileDownloadUtils;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private FarmUserServiceInter farmUserService;
    @Autowired
    private SkcKnowServiceInter skcKnowServiceImpl;
    @Autowired
    private LlmClientServiceInter llmClientServiceImpl;
    @Autowired
    private KnowIndexDocumentBuilder knowIndexDocumentBuilder;
    @Override
    public void handle(F2Event event) {
        //处理附件索引
        if (event.getAction().getType().equals(F2EActionT.EXTEND_FILE)) {
            String fileId = event.getObj().getId();
            String knowId = resourceFileServiceImpl.getBindAppId(fileId, Farm2RegisteTypeEnum.KNOW);
            Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneEmbeddingFilePath());
            SkcKnow know = skcKnowServiceImpl.getSkcKnowById(knowId);
            List<FarmDocument> documents = null;
            documents = knowIndexDocumentBuilder.getVectorDoc(knowId, fileId);
            indexService.deleteIndex(List.of(fileId), DocumentTitleEnum.fileId.name());
            indexService.createIndex(documents);
        }
        //删除附件索引
        if (event.getAction().getType().equals(F2EActionT.CANCEL_FILE)) {
            String fileId = event.getObj().getId();
            //只删除附件的索引
            Farm2LuceneIndexInter indexService = new Farm2LuceneIndexImpl(FarmLuceneUtils.getLuceneEmbeddingFilePath());
            indexService.deleteIndex(List.of(fileId),DocumentTitleEnum.fileId.name());
        }
    }


}
